package split;

import java.util.StringTokenizer;

public class SplitString {

	private int looptime = 10000;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String str = "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890"
				   + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
			       + "1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890";
		
		System.out.println("string length is :"+str.length());
		StringTokenizer tokenizer = new StringTokenizer(str,",");
		
		SplitString obj = new SplitString();
		
		long start = System.currentTimeMillis();
		
		obj.split_1(str);
		
		System.out.println("using split method :"+(System.currentTimeMillis()-start)+"ms");
		
		start = System.currentTimeMillis();
		
		obj.split_2(str, tokenizer);
		
		System.out.println("using tokenizer :"+(System.currentTimeMillis()-start)+"ms");
		
		
		start = System.currentTimeMillis();
		
		obj.split_3(str);
		
		System.out.println("using indexof & substring :"+(System.currentTimeMillis()-start)+"ms");
		
		
	}
	
	
	private void split_1(String str) {
		
		for(int n = 0; n < looptime;n++){
			
			str.split(",");
		
		}
		
	}
	
	
	private void split_2(String str,StringTokenizer tokenizer){
			
		for(int n = 0; n < looptime;n++){

			while(tokenizer.hasMoreElements()){
				tokenizer.nextToken();
			}
		}
	}
	
	
	private void split_3(String str){
		
		for(int n = 0; n < looptime;n++){

			String splitStr =  null;
			String tempStr = str;
			while(true){
				
				int i = tempStr.indexOf(",");
				
				if(i < 0){
					break;
				}
				
				splitStr = tempStr.substring(0,i);
				tempStr = tempStr.substring(i+1);
			}
		}
		
	}

}
